//
//  UIViewController+HKLeak.m
//  HKUtilities
//
//  Created by 胡锦涛 on 2018/11/3.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

#import "UIViewController+HKLeak.h"
#import "NSObject+HKSwizzling.h"
#import <objc/runtime.h>
const char * HKVCFLAG = "HKVCFLAG";
@implementation UIViewController (HKLeak)
/**
 * 1.交换方法
 */
+(void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleSEL:@selector(viewWillAppear:) withSEL:@selector(leak_viewWillAppear:)];
        [self swizzleSEL:@selector(viewDidDisappear:) withSEL:@selector(leak_viewDidDisAppear:)];
    });
}

- (void)leak_viewWillAppear:(BOOL)animate {
    [self leak_viewWillAppear:animate];
    //绑定属性
    objc_setAssociatedObject(self, HKVCFLAG, @(NO), OBJC_ASSOCIATION_ASSIGN);
}

- (void)leak_viewDidDisAppear:(BOOL)animate {
    [self leak_viewDidDisAppear:animate];
    if ([objc_getAssociatedObject(self, HKVCFLAG) boolValue]) {
        //pop
        //检车有没有被释放 （已经出栈）
        [self willDealloc];
    }
}

@end
